package com.zhangxin.designmode.designpattern.builder.v1;

import org.springframework.util.StringUtils;

import java.util.Objects;

/**
 * @author zhangxin
 * @date 2022/5/3
 */
public class ResourcePoolConfig {

    private static final int DEFAULT_MAX_TOTAL = 8;
    private static final int DEFAULT_MAX_IDLE = 8;
    private static final int DEFAULT_MIN_IDLE = 0;

    private String name;
    private int maxTotal = DEFAULT_MAX_TOTAL;
    private int maxIdle = DEFAULT_MAX_IDLE;
    private int minIdle = DEFAULT_MIN_IDLE;

    public ResourcePoolConfig(String name, Integer maxTotal, Integer maxIdle, Integer minIdle) {
        if (!StringUtils.hasLength(name)) {
            throw new IllegalArgumentException("name should not be empty.");
        }
        this.name = name;

        if (!Objects.isNull(maxTotal)) {
            if (maxTotal < 0) {
                throw new IllegalArgumentException("maxTotal should be positive.");
            }
            this.maxTotal = maxTotal;
        }

        if (!Objects.isNull(maxIdle)) {
            if (maxIdle < 0) {
                throw new IllegalArgumentException("maxIdle should not be negative.");
            }
            this.maxIdle = maxIdle;
        }

        if (!Objects.isNull(minIdle)) {
            if (minIdle < 0) {
                throw new IllegalArgumentException("minIdle should not be negative.");
            }
            this.maxIdle = maxIdle;
        }

    }

}
